package{
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.display.Shape;
	
	public class Letter extends Sprite {
		
		private var xPos:int;
		private var yPos:int;
		private var g_gridSpacing:int = 50;
		private var rect:Sprite = new Sprite();
		
		
		public function Letter():void {
			drawLetter();
		}
		
		private function drawLetter():void {
			rect.graphics.beginFill(0xFFCC00);
			rect.graphics.drawRect(0,0,50,50);
			rect.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown) 
			rect.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
			this.addChild(rect);
		}
		
		private function mouseDown(event:MouseEvent):void {
			this.startDrag();
		}
		
		private function mouseReleased(event:MouseEvent):void {
			this.stopDrag();
			yPos = this.y; // Reference Y Pos
			var tsf:testSnapFunction = (testSnapFunction) (parent);
			var letters:Array = tsf.getLetters();
			//trace("Deze letter: " + this.x);
			for(var i = 0; i < letters.length; i++){
				//check left side only
				if (letters[i] != this) {
					//trace("Letterobjecten: " + letters[i].x);
					if(letters[i].y - this.y < 20 && letters[i].y - this.y > - 20) {
						if (this.x - letters[i].x - 50  < 25 && this.x - letters[i].x - 50  > -25) {
							this.x = letters[i].x + 20 + 10 + 25;
						}
					}
				}
			}
			this.y = Math.round(yPos/g_gridSpacing) * g_gridSpacing;
		}
	}
}